/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.invoice.controller.reports;

/**
 *
 * @author laura
 */
import com.invoice.busines.ComprobanteBO;
import com.invoice.busines.ComprobanteBOImpl;
import com.invoice.busines.dto.ComprobanteDTO;
import com.invoice.exceptions.InvoiceException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;

@ManagedBean
public class ComprobanteReporte {    
    private static final Logger logger = Logger.getLogger(ComprobanteReporte.class.getName());
    ComprobanteDTO comprobanteDTO = new ComprobanteDTO();
    ComprobanteBO ComprobanteBO = new ComprobanteBOImpl();
    Map parameters;
    JRBeanCollectionDataSource beanColDataSource;

    public ComprobanteDTO getComprobanteDTO() {
        return comprobanteDTO;
    }

    public void setComprobanteDTO(ComprobanteDTO comprobanteDTO) {
        this.comprobanteDTO = comprobanteDTO;
    }
    
    public void printComprobante(){
        JasperPrint jasperPrint = null;
        String sourceFileName = "reports/newReport.jasper";
        beanColDataSource = new JRBeanCollectionDataSource(comprobanteDTO.getComprobantedetalles());
        parameters = new HashMap();
        parameters.put("tipo_comprobante", comprobanteDTO.getTipoComprobante());
        parameters.put("numero_comprobante", comprobanteDTO.getNumComp());
        parameters.put("fecha", comprobanteDTO.getFecha());
        parameters.put("nombre_cliente", comprobanteDTO.getClienteDTO().getNombre());
        parameters.put("direccion_cliente", comprobanteDTO.getClienteDTO().getDireccion());
        parameters.put("total", comprobanteDTO.getTotal());
        try {
            JasperCompileManager.compileReportToFile("reports/newReport.jrxml");
            jasperPrint = JasperFillManager.fillReport(
                    sourceFileName,
                    parameters,
                    beanColDataSource);
            JasperViewer jasperViewer = new JasperViewer(jasperPrint);
            jasperViewer.setVisible(true);
        } catch (JRException e) {
        }
    }
    public ComprobanteReporte() {
        try {
            JasperPrint jasperPrint = null;
            String sourceFileName = "reports/newReport.jasper";
            List<ComprobanteDTO> comprobantes = ComprobanteBO.findAll();
            JRBeanCollectionDataSource beanColDataSource =
                    new JRBeanCollectionDataSource(comprobantes.get(1).getComprobantedetalles());
            Map parameters = new HashMap();
            parameters.put("tipo_comprobante", comprobantes.get(1).getTipoComprobante());
            parameters.put("numero_comprobante", comprobantes.get(1).getNumComp());
            parameters.put("fecha", comprobantes.get(1).getFecha());
            parameters.put("nombre_cliente", comprobantes.get(1).getClienteDTO().getNombre());
            parameters.put("direccion_cliente", comprobantes.get(1).getClienteDTO().getDireccion());
            parameters.put("total", comprobantes.get(1).getTotal());
            try {
                JasperCompileManager.compileReportToFile("reports/newReport.jrxml");
                jasperPrint = JasperFillManager.fillReport(
                        sourceFileName,
                        parameters,
                        beanColDataSource);
                JasperViewer jasperViewer = new JasperViewer(jasperPrint);
                jasperViewer.setVisible(true);
            } catch (JRException e) {
            }
        } catch (InvoiceException ex) {
            Logger.getLogger(ComprobanteReporte.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String[] args) {
        new ComprobanteReporte();
        
    }
    
    
}
